package cc.angis.xiugallery.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import cc.angis.xiugallery.data.ImageData;

public class ImageDescDBHelper extends BaseDBHelper {
	
	public ImageDescDBHelper(){
		
	}
	
	public ImageDescDBHelper(Context context, String table){
		super(context, table);
	}
	
	public ImageDescDBHelper(Context context, String table, ContentValues values){
		super(context, table, values);
	}
	
	public long delete(int id){
		mWhereClaus = "id=?";
		mWhereArgs = new String[] { String.valueOf(id) };
		return delDB();
	}
	
	public long deleteByImgId(long imgid){
		mWhereClaus = "imgid=?";
		mWhereArgs = new String[] { String.valueOf(imgid) };
		return delDB();
	}
	
	public long deleteByImgIdAndType(long imgid, int type){
		mWhereClaus = "imgid=? AND type=?";
		mWhereArgs = new String[] { String.valueOf(imgid), String.valueOf(type) };
		return delDB();
	}
	
	public long insert(long imgid, int type, String descText, String voicePath){
			mValues = new ContentValues();
			mValues.put(TableConstants.ImageDescColumn.IMGID, imgid);
			mValues.put(TableConstants.ImageDescColumn.TYPE, type);
			mValues.put(TableConstants.ImageDescColumn.DESC_TEXT, descText);
			mValues.put(TableConstants.ImageDescColumn.VOICE_PATH, voicePath);
//			lContentValues.put(TableConstants.FavouriteImagesColumn.FAVNAME, favname);
			return insertDB();
	}
	
	public List<ImageData> getAllByImgId(long imgId){
		List<ImageData> imgDatas = new ArrayList<ImageData>();
		Cursor cursor = mDBHelper.getReadableDatabase().query(mTable, null, "imgid=?", new String[]{ String.valueOf(imgId)}, null, null, null);
		int idColumn = cursor.getColumnIndex(
				TableConstants.ImageDescColumn.ID);
		int imageIdColumn = cursor.getColumnIndex(
				TableConstants.ImageDescColumn.IMGID);
		int descTextColumn = cursor.getColumnIndex(
				TableConstants.ImageDescColumn.DESC_TEXT);
		int voicePathColumn = cursor.getColumnIndex(
				TableConstants.ImageDescColumn.VOICE_PATH);
		int typeColumn = cursor.getColumnIndex(
				TableConstants.ImageDescColumn.TYPE);
		if (cursor.moveToFirst()){
			ImageData imgData;
			do {
				imgData = new ImageData();
				imgData.setId(cursor.getInt(idColumn));
				imgData.setImgId(cursor.getLong(imageIdColumn));
				imgData.setDescText(cursor.getString(descTextColumn));
				imgData.setVoicePath(cursor.getString(voicePathColumn));
				imgData.setType(cursor.getInt(typeColumn));
				imgDatas.add(imgData);
			}while(cursor.moveToNext());
		}
		cursor.close();
		return imgDatas;
	}
	
	public long update(long imgId, int type, String descText, String voicePath){
		mWhereClaus = "imgid=? AND type=?";
		mWhereArgs = new String[] { String.valueOf(imgId),String.valueOf(type) };
		mValues = new ContentValues();
		if (descText != null)
			mValues.put(TableConstants.ImageDescColumn.DESC_TEXT, descText);
		if (voicePath !=null)
			mValues.put(TableConstants.ImageDescColumn.VOICE_PATH, voicePath);
		return updateDB();
	}
	
}
